<?php

class Site_Controller extends Controller{
    function __construct(){
        parent::Controller();
        
    }

    function index(){
        $data['content'] = "content/home";
        $this->load->view("include/template", $data);
    }
    
    function register(){ //status [STILL NEEDS TO BE MODIFIED]
        $this->load->model('site_model');
        $this->load->library('form_validation');
			
        $this->form_validation->set_rules('usernameR', 'Username', 'required|callback_username_check');
		$this->form_validation->set_rules('emailR', 'Email', 'required|valid_email|callback_email_check');
			
		if ($this->form_validation->run() == FALSE){ //form has some errors
			$data['content'] = "content/home";
            $this->load->view("include/template", $data);
		}
		else{ //form has no errors
			$now = date("Y-m-d H:i:s");
            $password = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
            $data = array( 
                'username' => $this->input->xss_clean($this->input->post('usernameR')),
                'password' => $password,
                'email' => $this->input->xss_clean($this->input->post('emailR')),
                'ipaddress' => $this->input->ip_address(),
                'stamp' => $now
            );
            
            $data['id'] = $this->site_model->save('users', $data); //this contains the user id of the newly created account!
            
            // START_SEND_EMAIL
            $config = Array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => 'yeheygames@gmail.com',
                'smtp_pass' => 'yeheypassword',
                'mailtype' => 'html'
            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            
            $this->email->from('yeheygames@gmail.com', 'Yehey Games');
            $this->email->to($data['email']);
            $this->email->subject('Yehey! Games - Account');
            $emailMsg = "<h1>Welcome to Yehey! Games</h1>".
                        "<p>Your'e account has been created!<br>" .
                        "Username: ". $data['username'] ."<br>".
                        "Password: " . $password . "</p>".
                        "<p>Login to the site now: <a href='". base_url() . "'>Yehey! Games</a></p>";
            $this->email->message($emailMsg);
            if($this->email->send()){
                echo 'Account created! Youre password was sent to: ' . $data['email'];
                
            }else{
                show_error($this->email->print_debugger());
            }
            // END_SEND_EMAIL
            $data['content'] = "content/register";
            $this->load->view("include/template", $data);
		}
        
    }    
    
    function login(){ //status [NOT YET OK]
        $this->load->model('site_model');
        
        $data['msg'] = $this->site_model->getAll('tz_members');
        $data['content'] = "content/login";
        $this->load->view("include/template", $data);
    }
    
    function username_check($str){ //status [OK]
        $this->load->model('site_model');
        $query = $this->site_model->get_by_field('users', 'username', $this->input->post('usernameR'));
        
		if ($query->num_rows() >= 1){
			$this->form_validation->set_message('username_check', 'The %s already exist!');
			return FALSE;
		}else{
			return TRUE;
		}
	}
    function email_check($str){ //status [OK]
        $this->load->model('site_model');
        $query = $this->site_model->get_by_field('users', 'email', $this->input->post('emailR'));
        
		if ($query->num_rows() >= 1){
			$this->form_validation->set_message('email_check', 'The %s already exist!');
			return FALSE;
		}else{
			return TRUE;
		}
	}
}